import { apiService } from './api'
import { API_ENDPOINTS } from '@/config/api'

// 设备接口
export interface Device {
  id: string
  sn: string
  product_id: string
  current_version: string
  last_check_time: string
  status: string
  created_at: string
  updated_at: string
  product: {
    id: string
    name: string
    public_id: string
  }
}

// 创建设备请求
export interface CreateDeviceRequest {
  sn: string
  product_id: string
  current_version?: string
}

// 更新设备请求
export interface UpdateDeviceRequest {
  sn?: string
  product_id?: string
  current_version?: string
  status?: string
}

// 设备服务
export const deviceService = {
  // 获取设备列表
  getDevices: (productId?: string, status?: string): Promise<Device[]> => {
    const params: any = {}
    if (productId) params.product_id = productId
    if (status) params.status = status
    return apiService.get<Device[]>(API_ENDPOINTS.DEVICES, params)
  },

  // 获取单个设备
  getDevice: (id: string): Promise<Device> => {
    return apiService.get<Device>(API_ENDPOINTS.DEVICE(id))
  },

  // 创建设备
  createDevice: (data: CreateDeviceRequest): Promise<Device> => {
    return apiService.post<Device>(API_ENDPOINTS.DEVICES, data)
  },

  // 更新设备
  updateDevice: (id: string, data: UpdateDeviceRequest): Promise<Device> => {
    return apiService.put<Device>(API_ENDPOINTS.DEVICE(id), data)
  },

  // 删除设备
  deleteDevice: (id: string): Promise<{ message: string }> => {
    return apiService.delete<{ message: string }>(API_ENDPOINTS.DEVICE(id))
  },

  // 更新设备状态
  updateDeviceStatus: (sn: string): Promise<{ message: string }> => {
    return apiService.put<{ message: string }>(API_ENDPOINTS.DEVICE_STATUS(sn), {})
  }
} 